//
//  NoticeView.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//
//

#import "NoticeView.h"

@interface NoticeView ()

@end

@implementation NoticeView

#pragma mark - lazy
- (UILabel *)labelNotice{
    if (!_labelNotice) {
        _labelNotice = [UILabel new];
        [GlobalMethod setLabel:_labelNotice widthLimit:SCREEN_WIDTH-60 numLines:0 lineSpace:2 isBold:false fontNum:14 textColor:COLOR_WHITE aligent:NSTextAlignmentCenter text:@"" bgColor:[UIColor clearColor]];
    }
    return _labelNotice;
}
- (UIView *)viewBG{
    if (!_viewBG) {
        _viewBG = [UIView new];
        _viewBG.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.6];
        [GlobalMethod setRoundView:_viewBG color:[UIColor clearColor] numRound:10 width:4];
        [_viewBG addSubview:self.labelNotice];
    }
    return _viewBG;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self addSubview:self.viewBG];
        self.userInteractionEnabled = false;
    }
    return  self;
}

- (void)showNotice:(NSString *)strNotice time:(CGFloat)timeShow frame:(CGRect)frame viewShow:(UIView *)viewShow {
    [self timerStop];

    self.frame = frame;
    
    // 设置label
    [self.labelNotice fitTitle:strNotice variable:SCREEN_WIDTH-60];
    self.viewBG.height = self.labelNotice.height+20;
    self.viewBG.width = self.labelNotice.width+40;
    
    self.viewBG.centerY = self.height/2;
    self.viewBG.centerX = self.width/2.0;
    self.labelNotice.center = CGPointMake(self.viewBG.width/2.0, self.viewBG.height/2.0);
    
    // 动画
    self.alpha = 0.0;
    self.transform = CGAffineTransformMakeScale(0.3, 0.3);
    [viewShow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
        self.transform = CGAffineTransformIdentity;
    }];
    
    // 定时器
    self.numTime = timeShow;
    [self timerStart];
}

- (void)timerStart{
    //开启定时器
    if (_timer == nil) {
        _timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    }
}

- (void)timerRun{
    //每秒的动作
    if (_numTime <=0) {
        //刷新按钮 开始
        [self timerStop];
        return;
    }
    _numTime --;
}

- (void)timerStop{
    //停止定时器
    if (_timer != nil) {
        [_timer invalidate];
        self.timer = nil;
    }
    [self removeFromSuperview];
}

- (void)dealloc{
    
}

@end
